package com.elai.web.controller.baseconfig;

import com.elai.baseconfig.domain.BaseApplicationInfoDO;
import com.elai.baseconfig.domain.BaseLesseeInfo;
import com.elai.baseconfig.service.IBaseLesseeInfoService;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.core.domain.entity.SysUser;
import com.elai.common.core.page.TableDataInfo;
import com.elai.common.enums.BusinessType;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.StringUtils;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.baseconfig.domain.EventTeamDO;
import com.elai.baseconfig.service.EventTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事件分组Controller
 *
 * @author elai
 * @date 2021-04-23
 */
@RestController
@RequestMapping("/config/eventteam")
public class EventTeamController extends BaseController
{
    @Autowired
    private EventTeamService eventTeamService;

    @Autowired
    private IBaseLesseeInfoService baseLesseeInfoService;

    /**
     * 查询事件分组列表
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:list')")
    @GetMapping("/list")
    public TableDataInfo list(EventTeamDO eventTeam)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventTeam.setLesseeId(currentUser.getLesseeId());
        }
        startPage();
        List<EventTeamDO> list = eventTeamService.selectEventTeamList(eventTeam);
        return getDataTable(list);
    }

    @GetMapping("/getAllTeamList")
    public AjaxResult getAllTeamList(EventTeamDO eventTeam)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventTeam.setLesseeId(currentUser.getLesseeId());
        }
        eventTeam.setStatus("Y");
        List<EventTeamDO> list = eventTeamService.selectEventTeamList(eventTeam);
        return AjaxResult.success(list);
    }

    /**
     * 导出事件分组列表
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:export')")
    @Log(title = "事件分组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EventTeamDO eventTeam)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventTeam.setLesseeId(currentUser.getLesseeId());
        }
        List<EventTeamDO> list = eventTeamService.selectEventTeamList(eventTeam);
        ExcelUtil<EventTeamDO> util = new ExcelUtil<EventTeamDO>(EventTeamDO.class);
        return util.exportExcel(list, "eventteam");
    }

    /**
     * 获取事件分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(eventTeamService.selectEventTeamById(id));
    }

    /**
     * 新增事件分组
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:add')")
    @Log(title = "事件分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventTeamDO eventTeam)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(StringUtils.isNotEmpty(currentUser.getLesseeId())){
            BaseLesseeInfo lesseeInfo= baseLesseeInfoService.selectBaseLesseeInfoByLesseeId(currentUser.getLesseeId());
            if(lesseeInfo==null){
                return AjaxResult.error("添加分组'" + eventTeam.getTeamName() + "'失败，租户ID不存在");
            }
            if("02".equals(lesseeInfo.getStatus())){
                return AjaxResult.error("添加分组'" + eventTeam.getTeamName() + "'失败，租户异常请联系管理员");
            }
            if(lesseeInfo.getBeginDate().getTime()> DateUtils.getNowDate().getTime() || DateUtils.getNowDate().getTime()>lesseeInfo.getEndDate().getTime()){
                return AjaxResult.error("添加分组'" +eventTeam.getTeamName() + "'失败，租户不在有效期内，请联系管理员");
            }
            eventTeam.setLesseeId(currentUser.getLesseeId());
            EventTeamDO param=new EventTeamDO();
            param.setLesseeId(currentUser.getLesseeId());
            param.setTeamCode(eventTeam.getTeamCode());
            List<EventTeamDO> apps = eventTeamService.selectEventTeamList(param);
            if(apps.size()>0){
                return AjaxResult.error("添加分组'" + eventTeam.getTeamName() + "'失败，原因：分组已经存在，不能重复添加");
            }
        }else{
            return AjaxResult.error("添加分组'" + eventTeam.getTeamName()  + "'失败，请联系管理员配置租户");
        }
        return toAjax(eventTeamService.insertEventTeam(eventTeam));
    }

    /**
     * 修改事件分组
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:edit')")
    @Log(title = "事件分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventTeamDO eventTeam)
    {
        return toAjax(eventTeamService.updateEventTeam(eventTeam));
    }

    /**
     * 删除事件分组
     */
    @PreAuthorize("@ss.hasPermi('config:eventteam:remove')")
    @Log(title = "事件分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eventTeamService.deleteEventTeamByIds(ids));
    }
}
